package vip.ifmm.lehouse.file.service.api.impl;

import com.alibaba.dubbo.config.annotation.Service;
import org.springframework.util.StringUtils;
import vip.ifmm.lehouse.common.dto.ServerResponse;
import vip.ifmm.lehouse.common.enums.status.FileStatus;
import vip.ifmm.lehouse.common.model.Enclosure;
import vip.ifmm.lehouse.file.service.api.EnclosureService;
import vip.ifmm.lehouse.file.service.dao.EnclosureDao;

import javax.annotation.Resource;
import java.util.Date;

/**
 * @author: mackyhuang
 * <p>email: mackyhuang@163.com <p>
 * <p>date: 2019/6/18 </p>
 */

@Service(owner = "mackyhuang",
    mock = "vip.ifmm.lehouse.file.service.api.mock.EnclosureServiceMock")
public class EnclosureServiceImpl implements EnclosureService {

    @Resource
    private EnclosureDao enclosureDao;

    /**
     * 文件信息存储至数据库
     * @param enclosure
     * @return
     */
    @Override
    public ServerResponse saveEnclosureInfo(Enclosure enclosure) {
        if (enclosure == null ||
        StringUtils.isEmpty(enclosure.getEnclosureName()) ||
        StringUtils.isEmpty(enclosure.getEnclosurePath()) ||
        StringUtils.isEmpty(enclosure.getEnclosureSize()) ||
        StringUtils.isEmpty(enclosure.getEnclosureSuffix())){
            return ServerResponse.response(FileStatus.FILE_INFO_LACKED);
        }
        enclosure.setEnclosureCreateTime(new Date());
        enclosure.setEnclosureUpdateTime(new Date());
        int result = enclosureDao.saveEnclosure(enclosure);
        if (result <= 0){
            return ServerResponse.response(FileStatus.FILE_SAVE_FAIL);
        }
        return ServerResponse.response(FileStatus.FILE_SAVE_SUCCESS);
    }
}
